package ruifeng.zcj.dig.ui.home;

import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.Bind;
import butterknife.OnClick;
import ruifeng.zcj.dig.R;
import ruifeng.zcj.dig.base.DepositFragmentNoTitleActivity;
import ruifeng.zcj.dig.base.FragmentInfo;
import ruifeng.zcj.dig.ui.assessment.AssessmentFragment;
import ruifeng.zcj.dig.ui.forum.ForumFragment;
import ruifeng.zcj.dig.ui.shopping.ShoppingFragment;
import ruifeng.zcj.dig.ui.vehicle.VehicleFragment;

/**
 * Created by Administrator on 2016/9/26 0026.
 */
public class MainActivity2 extends DepositFragmentNoTitleActivity {
    @Bind(R.id.btn_tab_1)
    ImageView iv_tab_1;
    @Bind(R.id.btn_tab_2)
    ImageView iv_tab_2;
    @Bind(R.id.btn_tab_3)
    ImageView iv_tab_3;
    @Bind(R.id.btn_tab_4)
    ImageView iv_tab_4;
    @Bind(R.id.btn_tab_5)
    ImageView iv_tab_5;
    @Bind(R.id.tab_btn_menu)
    LinearLayout mTabBtnMenu;


    @Override
    protected void addPage(ArrayList<String> tabTitles, ArrayList<FragmentInfo> fragmentInfos) {
        tabTitles.add("首页");
        fragmentInfos.add(new FragmentInfo(HomeFragment.class, getBundle(1)));
       // fragmentInfos.add(new FragmentInfo(ShoppingFragment.class, getBundle(1)));

        tabTitles.add("商城");
        fragmentInfos.add(new FragmentInfo(ShoppingFragment.class, getBundle(1)));

        tabTitles.add("论坛");
        fragmentInfos.add(new FragmentInfo(ForumFragment.class, getBundle(1)));

        tabTitles.add("评估");
        fragmentInfos.add(new FragmentInfo(AssessmentFragment.class, getBundle(1)));

        tabTitles.add("二手车");
        fragmentInfos.add(new FragmentInfo(VehicleFragment.class, getBundle(1)));
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_home2;
    }

    public void switchFragment(int position) {
        switch (position) {
            case 0:// 首页的切换
                iv_tab_1.setSelected(true);
                iv_tab_2.setSelected(false);
                iv_tab_3.setSelected(false);
                iv_tab_4.setSelected(false);
                iv_tab_5.setSelected(false);
                setCurrentItem(0);
                break;
            case 1:
                iv_tab_1.setSelected(false);
                iv_tab_2.setSelected(true);
                iv_tab_3.setSelected(false);
                iv_tab_4.setSelected(false);
                iv_tab_5.setSelected(false);
                setCurrentItem(1);
                break;
            case 2:
                iv_tab_1.setSelected(false);
                iv_tab_2.setSelected(false);
                iv_tab_3.setSelected(true);
                iv_tab_4.setSelected(false);
                iv_tab_5.setSelected(false);
                setCurrentItem(2);
                break;
            case 3:
                iv_tab_1.setSelected(false);
                iv_tab_2.setSelected(false);
                iv_tab_3.setSelected(false);
                iv_tab_4.setSelected(true);
                iv_tab_5.setSelected(false);
                setCurrentItem(3);
                break;
            case 4:
                iv_tab_1.setSelected(false);
                iv_tab_2.setSelected(false);
                iv_tab_3.setSelected(false);
                iv_tab_4.setSelected(false);
                iv_tab_5.setSelected(true);
                setCurrentItem(4);
                break;
            case 5:
                break;
            default:
                break;
        }
    }

    @Override
    public void initView() {
        super.initView();
        switchFragment(0);

       // StatusBarUtil.setTransparent(this);
    }

    @OnClick({R.id.btn_tab_1, R.id.btn_tab_2, R.id.btn_tab_3, R.id.btn_tab_4, R.id.btn_tab_5})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_tab_1:
                switchFragment(0);
                break;
            case R.id.btn_tab_2:
                switchFragment(1);
                break;
            case R.id.btn_tab_3:
                switchFragment(2);
                break;
            case R.id.btn_tab_4:
                switchFragment(3);
                break;
            case R.id.btn_tab_5:
                switchFragment(4);
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exitBy2Click();
        }
        return false;
    }

    private static Boolean isExit = false;

    private void exitBy2Click() {
        Timer tExit = null;
        if (isExit == false) {
            isExit = true;
            showSnackBar("再按一次退出程序");
            tExit = new Timer();
            tExit.schedule(new TimerTask() {
                @Override
                public void run() {
                    isExit = false;
                }
            }, 2000);
        } else {
            finish();
            System.exit(0);
        }
    }

}
